package com.qf.dao.impl;

import com.qf.dao.OrderDao;
import com.qf.pojo.Order;
import com.qf.pojo.OrderItem;
import com.qf.utils.JdbcUtils;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author Kuke
 * @date 2022/3/12 12:10
 */
public class OrderDaoImpl  implements OrderDao {

    /**
     * 插入订单
     * @param order
     */
    @Override
    public void insertOrder(Order order) throws SQLException {


        //dao层:使用执行对象的手动提交方式 :不能自动提交
        //使用QueryRunner空参构造
//          public QueryRunner() {
//        }
        //执行对象
        QueryRunner qr = new QueryRunner() ;
        //sql
        String sql = "insert into orders values(?,?,?,?,?,?,?,?)" ;

        ///执行对象.update(Connnection 对象，sql,实际参数)
        int count = qr.update(JdbcUtils.getConnection(), sql,
                order.getOid(),
                order.getOrdertime(),
                order.getTotal(),
                order.getState(),
                order.getAddress(),
                order.getName(),
                order.getTelephone(),
                order.getUser().getUid());
        System.out.println(count);




    }

    /**
     * 插入订单项
     * @param orderitem
     */
    @Override
    public void insertOrderItem(OrderItem orderitem) throws SQLException {

        //执行对象
        QueryRunner qr = new QueryRunner() ;
        //sql
        String sql = "insert into orderitem values(?,?,?,?,?)" ;

        ///执行对象.update(Connnection 对象，sql,实际参数)
        int count = qr.update(JdbcUtils.getConnection(), sql,
                orderitem.getItemid(),
                orderitem.getCount(),
                orderitem.getSubtotal(),
                orderitem.getProduct().getPid(),
                orderitem.getOrder().getOid()) ;
        System.out.println(count);

    }

    //更新订单重点 用户的数据
    @Override
    public int update(Order order) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        String sql = "update orders set address =? ,name =? ,telephone = ? where oid = ?" ;
        return  qr.update(sql,order.getAddress(),order.getName(),order.getTelephone(),order.getOid()) ;
    }

    /**
     * 通过订单编号获取订单
     * @param orderNO
     * @return
     */
    @Override
    public Order selectOrderById(String orderNO) throws SQLException {
        //执行对象
        QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource()) ;
        String sql = "select * from orders where oid = ?" ;
        Order order = queryRunner.query(sql, new BeanHandler<Order>(Order.class), orderNO);
        return order;
    }

    /**
     * 订单数据访问接口:更新订单状态
     * @param order
     */
    @Override
    public void updateState(Order order) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
        String sql = "update orders set state  = ? where oid = ?" ;
        int count    = qr.update(sql, order.getState(), order.getOid());
        System.out.println(count);
    }
}
